<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择销售合同"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <salesContract ref="salesContract"></salesContract>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import salesContract from "@/views/salesinfo/sales_contract/q_sales_contract.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    salesContract,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.salesContract.searchFormFields.Voucher_sub = null;
      this.$refs.salesContract.searchFormFields.Client = null;
      this.$refs.salesContract.searchFormFields.AuditStatus = "1";
      this.$refs.salesContract.searchFormFields.Check_out_flag = null;
      this.$refs.salesContract.searchFormFields.Delivery_mode = null;
      this.$refs.salesContract.searchFormFields.Delivery_time = [null,null];
      this.$refs.salesContract.searchFormFields.Signed_num = null;
      this.$refs.salesContract.searchFormFields.Contract_intro = null;
    },
    open(off = true,Client) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.salesContract && off) {
          if(Client){
          this.$refs.salesContract.searchFormFields.Client = Client
          this.$refs.salesContract.searchFormOptions.forEach(item => {
            item.forEach(x=>{
              if (x.field=="Client") {
                x.disabled=true
              }
            })
          });
        }
          clearInterval(setIntervalTiem);
          // this.$refs.salesContract.searchFormFields.Delivery_time = [
          //   this.base.getDate(),
          //   "",
          // ];
          if(this.$route.path == "/q_after_delivery"){
            this.$refs.salesContract.searchFormFields.Zhibao_state = "1";
            this.$refs.salesContract.searchFormFields.Delivery_time = [,];
          }
          if(this.$route.path == "/q_delivery_request"){
            this.$refs.salesContract.searchFormFields.Logistics_status = "0";
            this.$refs.salesContract.searchFormFields.Delivery_time = [,];
          }
          this.$refs.salesContract.searchFormFields.AuditStatus = "1";
          this.$refs.salesContract.$refs.grid.search();
        }
      }, 100);
    },

    onSelect() {
      let rowObj = this.$refs.salesContract.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        this.base.ElMessageFn("请选择一条数据");
        return (this.model = true);
      }
      if(this.$route.path == "/q_sales" ){
          if(rowObj[0].Sales_state!=0){
            this.base.ElMessageFn("请选择未销售单据")
            return ;
          }
      }
      if(this.$route.path == "/q_after_delivery" ){
          if(rowObj[0].Zhibao_state!=1){
            this.base.ElMessageFn("请选择质保中单据")
             return ;
          }
      }
      if(this.$route.path == "/q_delivery_request" ){
          if(rowObj[0].Logistics_status!="0"){
            this.base.ElMessageFn("请选择未发货单据")
             return ;
          }
      }
      let AuditStatusSome = rowObj.some((item) => item.AuditStatus != 1);
      if (AuditStatusSome) {
        this.base.ElMessageFn("请勿选择审核未通过单据","warning") 
         return;
      }
      this.$emit("parentCall", ($parent) => {
        if(this.$route.path == "/q_after_delivery" ){
          $parent.editFormFields.Sales_contract_sub =
            rowObj[0].Voucher_sub;//编码
          $parent.editFormFields.Sales_contract = rowObj[0].Sales_contract;//ID
          // $parent.editFormFields.Site = 123;//站点无，要去掉
          $parent.editFormFields.Tax_rate =
            ""+rowObj[0].Tax_rate;//税率
          if (rowObj[0].Client) {
            $parent.editFormFields.Client_name = JSON.stringify(rowObj[0].Client);//客户
          }
          if (rowObj[0].WHSE_no) {
            $parent.editFormFields.WHSE_no = rowObj[0].WHSE_no;//机构
          }
      }
        if (this.$route.path == "/q_payment_receipt") {
          $parent.editFormFields.Sales_contract_voucher_sub =
            rowObj[0].Voucher_sub;
          $parent.editFormFields.Total_price = rowObj[0].Signed_price;
        }
        if (this.$route.path == "/q_delivery_request") {
          $parent.editFormFields.Sales_contract_sub =
            rowObj[0].Voucher_sub;
          $parent.editFormFields.Sales_contract = rowObj[0].Sales_contract;
          $parent.editFormFields.Client = JSON.stringify(rowObj[0].Client);
          $parent.editFormFields.Project_name = rowObj[0].Project_name;
          $parent.editFormFields.Delivery_address = rowObj[0].Delivery_address;
          // $parent.editFormFields.Delivery_address = rowObj[0].Delivery_address;
          // $parent.editFormFields.Tax_rate =
          //   ""+rowObj[0].Tax_rate;
          // if (rowObj[0].Delivery_mode) {
          //   $parent.editFormFields.Delivery_mode = JSON.stringify(
          //     rowObj[0].Delivery_mode
          //   );
          // }
          // if (rowObj[0].Client) {
          //   $parent.editFormFields.Client = JSON.stringify(rowObj[0].Client);
          // }
       
        }
        if(this.$route.path == "/q_sales" ){
          $parent.editFormFields.Sales_contract_voucher_sub =rowObj[0].Voucher_sub;
          $parent.editFormFields.Sales_contract_id = rowObj[0].Sales_contract;
          $parent.editFormFields.Delivery_address = rowObj[0].Delivery_address;
          $parent.editFormFields.WHSE_no = rowObj[0].WHSE_no;
          $parent.editFormFields.Project_name = rowObj[0].Project_name;
          $parent.editFormFields.Tax_rate =
          $parent.editFormFields.Install_service = rowObj[0].Install_service!=null?""+rowObj[0].Install_service:"";
          $parent.editFormFields.Install_status = rowObj[0].Install_service==0?"0":"3";
          $parent.editFormFields.Tax_rate =
            ""+rowObj[0].Tax_rate;
          if (rowObj[0].Delivery_mode) {
            $parent.editFormFields.Delivery_mode = JSON.stringify(
              rowObj[0].Delivery_mode
            );
          }
          if (rowObj[0].Client) {
            $parent.editFormFields.Client = JSON.stringify(rowObj[0].Client);
          }
            $parent.editFormFields.Goods_num = rowObj[0].Signed_num
            $parent.editFormFields.Total_price = rowObj[0].Signed_price
            $parent.WHSE_IdDisableFn(true);//特殊的处理库位和税率
            // 发送请求
            try {
              this. http.post('api/q_sales_contractList/Getsales_contractList?fID='+rowObj[0].Sales_contract, {}, "").then(reslut => {
              console.log("获取明细表数据", reslut)
                if (reslut.length) {
                  this.$emit("parentCall", ($parent) => {
                    reslut.forEach(item=>{
                      item.Sales_contract_voucher_sub= $parent.editFormFields.Sales_contract_voucher_sub
                      item.Tax_exclusive_contract_total_price=item.Tax_exclusive_total_price
                    })
                    $parent.$refs.detail.rowData.unshift(...reslut);
                  })
                }         
              })
            } catch (error) {
              console.log(error)
            }
        }
        if( this.$route.path == "/q_install_request"){
          $parent.editFormFields.Sales_contract_voucher_sub =
            rowObj[0].Voucher_sub;
            $parent.editFormFields.Install_address =
            rowObj[0].Delivery_address;
            $parent.editFormFields.Install_date =
            rowObj[0].Delivery_time;
            $parent.editFormFields.Client =
            JSON.stringify(rowObj[0].Client);
            $parent.editFormFields.Sales_contract_id =
            rowObj[0].Sales_contract;
        }
        // 升级密码生成
        if( this.$route.path == "/q_serve_uppass" || this.$route.path == "/q_serve_advancPass" || this.$route.path == "/q_serve_gcLicense" || this.$route.path == "/q_serve_qhLicense"){
          // $parent.editFormFields.Voucher_sub = rowObj[0].Voucher_sub;
          $parent.editFormFields.Sales_contract_voucher_sub = rowObj[0].Voucher_sub;
          $parent.editFormFields.Client = JSON.stringify(rowObj[0].Client);
          $parent.editFormFields.Project_name = rowObj[0].Project_name;
        }
        // 高级密码生成
        // if( this.$route.path == "/q_serve_advancPass"){
        //   // $parent.editFormFields.Voucher_sub = rowObj[0].Voucher_sub;
        //   $parent.editFormFields.Sales_contract_voucher_sub = rowObj[0].Voucher_sub;
        //   $parent.editFormFields.Client = JSON.stringify(rowObj[0].Client);
        //   $parent.editFormFields.Project_name = rowObj[0].Project_name;
        // }
        // if (!$parent.$refs.detail.rowData.length) {
        //   $parent.$refs.detail.rowData.unshift(...rowObj);
        // } else {
        //   $parent.$refs.detail.rowData.map((item) => {
        //     if (item.Voucher_sub == rowObj[0].Voucher_sub) {
        //       this.$message.error("单据中已包含该合同");
        //     } else {
        //       $parent.$refs.detail.rowData.unshift(...rowObj);
        //     }
        //   });
        // }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>